package com.ucar.umock.client.exector.support;

import com.ucar.umock.client.dto.UMockMethodRuntimeInfoDTO;
import com.ucar.umock.client.util.UMockTypeUtil;
import com.ucar.umock.client.config.UMockMethodConfigBean;
import com.ucar.umock.client.enums.EnumMockExectorType;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Component;

import java.util.Random;

/**
 * @ClassName UMockValueFixedSimpleRandomHandle
 * @Description TODO
 * @Author zhanbs baoshan.zhan@ucarinc.com
 * @Date 2020/4/3 15:34
 * @Version 1.0
 **/
@Component
public class UMockValueFixedSimpleRandomExector extends UMockValueBaseExector {
    public Object exec(UMockMethodConfigBean config, UMockMethodRuntimeInfoDTO methodRuntimeDTO) {
        Class retuenClass=methodRuntimeDTO.getReturnClass();
        Random random=new Random();
        if(UMockTypeUtil.isBoolean(retuenClass)){
            return random.nextBoolean();
        }else if(UMockTypeUtil.isInt(retuenClass)){
            return random.nextInt();
        }else if(UMockTypeUtil.isByte(retuenClass)){
            byte[] nbyte=new byte[1];
            random.nextBytes(nbyte);
            return new Byte(nbyte[0]);
        }else if(UMockTypeUtil.isShort(retuenClass)){
            Integer val=random.nextInt(10);
            return val.shortValue();
        }else if(UMockTypeUtil.isLong(retuenClass)){
            return random.nextLong();
        }else if(UMockTypeUtil.isCharacter(retuenClass)){
            String str= RandomStringUtils.random(1);
            return str.charAt(0);
        }else if(UMockTypeUtil.isFloat(retuenClass)){
            return random.nextFloat();
        }else if(UMockTypeUtil.isDouble(retuenClass)){
            return random.nextDouble();
        }else if(UMockTypeUtil.isString(retuenClass)){
            int count=random.nextInt(3);
            return RandomStringUtils.random(count,true,true);
        }else{
            return null;
        }
    }

    public EnumMockExectorType getType() {
        return EnumMockExectorType.FIXED_SIMPLE_RANDOM;
    }
}
